home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 19 / CD_ASCQ_19_010295.iso / dos / prg / pas / swag / win_os2.swg / 0023_Printer Controls.pas < prev    next >
Pascal/Delphi Source File  |  1994-05-26  |  8KB  |  249 lines

  1. {************************************************}
  2. {                                                }
  3. {   AJC Printer Unit for Windows                 }
  4. {                                                }
  5. {   Printer control constants/functions          }
  6. {                                                }
  7. {   Author:  Andrew J. Cook                      }
  8. {            Omaha, NE                           }
  9. {            CompuServe ID:  71331,501           }
  10. {                                                }
  11. {   Written: January 1994                        }
  12. {                                                }
  13. {   Copyright:  None!  I hereby commit this unit }
  14. {                      to the public domain.     }
  15. {                                                }
  16. {************************************************}
  17.  
  18. {************************************************}
  19. {                                                }
  20. {  New SetPageSize function added and changed    }
  21. {  margin code in SetPrintParams function.       }
  22. {                                                }
  23. {  Modified by:                                  }
  24. {               Paul Mayer                       }
  25. {               ZPAY Payroll Systems, Inc.       }
  26. {               St. Petersburg,  FL              }
  27. {               CompuServe ID: 76711,1141        }
  28. {                                                }
  29. {  Thanks to Kurt Barthelmess Borland Team B for }
  30. {  pointing out what I was doing wrong so I      }
  31. {  could get this function to work after a week  }
  32. {  of trial and error and a lot of test paper!   }
  33. {                                                }
  34. {                                 April 1994     }
  35. {                                                }
  36. {************************************************}
  37.  
  38. unit AJCPrntW;
  39.  
  40. {$F+,O+,S-}
  41.  
  42. interface
  43.  
  44. uses WinTypes, WinProcs, OPrinter;
  45.  
  46. type
  47.   PAJCPrinter = ^TAJCPrinter;
  48.   TAJCPrinter = object(TPrinter)
  49.     function SetPageOrientation(Orientation:  Integer): Integer; virtual;
  50.     function SetPageSize(PageID, NewLength, NewWidth : Integer) : Integer; virtual;
  51.   end;
  52.  
  53. const
  54.   pm_Size = 1;
  55.   pm_Print = 2;
  56.  
  57. type
  58.   PAJCPrintOut = ^TAJCPrintOut;
  59.   TAJCPrintOut = object(TPrintOut)
  60.     VUnitsPerInch:  Integer;
  61.     HUnitsPerInch:  Integer;
  62.     LMarginUnits:  Integer;
  63.     TMarginUnits:  Integer;
  64.     RMarginUnits:  Integer;
  65.     BMarginUnits:  Integer;
  66.     OriginalAlignmentOptions:  Word;
  67.     constructor Init(ATitle:  PChar);
  68.     destructor Done; virtual;
  69.     procedure SetPrintParams(ADC: HDC; ASize: TPoint); virtual;
  70.     function VLogPos(Pos:  Integer): Integer; virtual;
  71.     function HLogPos(Pos:  Integer): Integer; virtual;
  72.     function VInches(Inches: Real): Integer; virtual;
  73.     function HInches(Inches: Real): Integer; virtual;
  74.     function Points(APoints:  Integer): Integer; virtual;
  75.     function PrintHeader(Mode, Page:  Word): Integer; virtual;
  76.     function PrintFooter(Mode, Page:  Word): Integer; virtual;
  77.     procedure JustifyLeft;
  78.     procedure JustifyCenter;
  79.     procedure JustifyRight;
  80.   end;
  81.  
  82. var
  83.   DevModeOut, DevModeIn : PDevMode;
  84.  
  85. implementation
  86.  
  87. function TAJCPrinter.SetPageOrientation(Orientation: Integer): Integer;
  88. var
  89.   DevMode:  PDevMode;
  90.   Result:  Integer;
  91. begin
  92.   SetPageOrientation := -1;
  93.   if (Orientation <> dmOrient_Portrait) and
  94.      (Orientation <> dmOrient_Landscape) then
  95.        exit;
  96.   if @ExtDeviceMode = nil then exit;
  97.   if DevSettings^.dmFields or dm_Orientation = 0 then exit;
  98.  
  99.   if DevSettings^.dmOrientation = Orientation then
  100.     begin
  101.       SetPageOrientation := 1;
  102.       exit;
  103.     end;
  104.  
  105.   GetMem(DevMode, DevSettingSize);
  106.   Move(DevSettings^, DevMode^, DevSettingSize);
  107.   DevMode^.dmOrientation := Orientation;
  108.   Result := ExtDeviceMode(0, DeviceModule, DevSettings^, Device, Port,
  109.                           DevMode^, nil, dm_In_Buffer or dm_Out_Buffer);
  110.   FreeMem(DevMode, DevSettingSize);
  111.   if Result = IDOK then
  112.     SetPageOrientation := 0;
  113. end;
  114.  
  115. function TAJCPrinter.SetPageSize(PageID, NewLength, NewWidth : Integer): Integer;
  116. var
  117.   DevModeIn:  PDevMode;
  118.   Result:  Integer;
  119.   Size : Integer;
  120. begin
  121.   SetPageSize := -1;
  122.   if @ExtDeviceMode = nil then exit;
  123.   GetMem(DevModeIn, DevSettingSize);
  124.   Result := ExtDeviceMode(0, DeviceModule, DevSettings^, Device, Port,
  125.                           DevModeIn^, nil, dm_Out_Buffer);
  126.   DevModeIn^.dmDeviceName := DevSettings^.dmDeviceName;
  127.   DevModeIn^.dmSpecVersion := DevSettings^.dmSpecVersion;
  128.   DevModeIn^.dmDriverVersion := 0;
  129.   DevModeIn^.dmFields := dm_PaperSize or dm_Paperlength or dm_PaperWidth;
  130.   DevModeIn^.dmPaperSize := PageId {eg dmPaper_User, dmPaper_Letter};
  131.   DevModeIn^.dmPaperLength := NewLength; {in 1/10 of millimeters}
  132.   DevModeIn^.dmPaperWidth := NewWidth {in 1/10 of millimeters};
  133.   Result := ExtDeviceMode(0, DeviceModule, DevSettings^, Device, Port,
  134.                           DevModeIn^, nil, dm_In_Buffer or dm_Out_Buffer);
  135.   FreeMem(DevModeIn, DevModeIn^.dmSize + DevModeIn^.dmDriverExtra);
  136.   if Result = IDOK then
  137.     SetPageSize := 0;
  138. end;
  139.  
  140. constructor TAJCPrintOut.Init(ATitle:  PChar);
  141. begin
  142.   inherited Init(ATitle);
  143.   OriginalAlignmentOptions := 0;
  144. end;
  145.  
  146. destructor TAJCPrintOut.Done;
  147. begin
  148.   if OriginalAlignmentOptions <> 0 then
  149.     SetTextAlign(DC, OriginalAlignmentOptions);
  150.  
  151.   inherited Done;
  152. end;
  153.  
  154. procedure TAJCPrintOut.SetPrintParams(ADC: HDC; ASize: TPoint);
  155. var
  156.   lpOffset, lpDimensions : TPoint;
  157. begin
  158.   inherited SetPrintParams(ADC, ASize);
  159.  
  160.   OriginalAlignmentOptions := GetTextAlign(DC);
  161.  
  162.   VUnitsPerInch := GetDeviceCaps(DC, LogPixelsY);
  163.   HUnitsPerInch := GetDeviceCaps(DC, LogPixelsX);
  164.  
  165.   Escape(DC, GetPhysPageSize, 0, nil, @lpDimensions);
  166.   Escape(DC, GetPrintingOffset, 0, nil, @lpOffset);
  167.  
  168.   TMarginUnits := lpOffset.Y;
  169.   LMarginUnits := lpOffset.X;
  170.   BMarginUnits := (lpDimensions.Y - (Size.Y+lpOffset.Y));
  171.   RMarginUnits := (lpDimensions.X - (Size.X+lpOffset.X));
  172. end;
  173.  
  174. function TAJCPrintOut.VLogPos(Pos: Integer): Integer;
  175. begin
  176.   if Pos < 0 then
  177.     VLogPos := Size.Y + Pos + TMarginUnits
  178.   else
  179.     VLogPos := Pos - TMarginUnits;
  180. end;
  181.  
  182.  
  183. function TAJCPrintOut.HLogPos(Pos: Integer): Integer;
  184. begin
  185.   if Pos < 0 then
  186.     HLogPos := Size.X + Pos + LMarginUnits
  187.   else
  188.     HLogPos := Pos - LMarginUnits;
  189. end;
  190.  
  191. function TAJCPrintOut.VInches(Inches: Real): Integer;
  192. begin
  193.   VInches := round(Inches * VUnitsPerInch);
  194. end;
  195.  
  196. function TAJCPrintOut.HInches(Inches: Real): Integer;
  197. begin
  198.   HInches := round(Inches * HUnitsPerInch);
  199. end;
  200.  
  201. function TAJCPrintOut.Points(APoints:  Integer): Integer;
  202. begin
  203.   Points := APoints * (VUnitsPerInch) div 72;
  204. end;
  205.  
  206. function TAJCPrintOut.PrintHeader(Mode, Page:  Word):  Integer;
  207. begin
  208.   PrintHeader := 0;
  209. end;
  210.  
  211. function TAJCPrintOut.PrintFooter(Mode, Page:  Word):  Integer;
  212. begin
  213.   PrintFooter := 0;
  214. end;
  215.  
  216. procedure TAJCPrintOut.JustifyLeft;
  217. var
  218.   AlignmentOptions:  Word;
  219. begin
  220.   AlignmentOptions := GetTextAlign(DC);
  221.   AlignmentOptions := AlignmentOptions and not (ta_left or ta_center or ta_right);
  222.   AlignmentOptions := AlignmentOptions or ta_left;
  223.   SetTextAlign(DC, AlignmentOptions);
  224. end;
  225.  
  226. procedure TAJCPrintOut.JustifyCenter;
  227. var
  228.   AlignmentOptions:  Word;
  229. begin
  230.   AlignmentOptions := GetTextAlign(DC);
  231.   AlignmentOptions := AlignmentOptions and not (ta_left or ta_center or ta_right);
  232.   AlignmentOptions := AlignmentOptions or ta_center;
  233.   SetTextAlign(DC, AlignmentOptions);
  234. end;
  235.  
  236. procedure TAJCPrintOut.JustifyRight;
  237. var
  238.   AlignmentOptions:  Word;
  239. begin
  240.   AlignmentOptions := GetTextAlign(DC);
  241.   AlignmentOptions := AlignmentOptions and not (ta_left or ta_center or ta_right);
  242.   AlignmentOptions := AlignmentOptions or ta_right;
  243.   SetTextAlign(DC, AlignmentOptions);
  244. end;
  245.  
  246.  
  247. begin
  248. end.
  249.